package com.aidong.myprofile.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.myprofile.biz.MySportLablesBiz;
import com.aidong.myprofile.entity.MySportLables;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 运动标签action
 * @author user
 *
 */
@ParentPackage("json")
@Namespace("/")
public class MySportLablesAction implements ModelDriven<MySportLables>{
	
	private MySportLables sportLables;
	
	@Override
	public MySportLables getModel() {
		if(sportLables == null){
			sportLables = new MySportLables();
		}
		return sportLables;
	}

	private MySportLablesBiz sportLablesBiz;

	@Autowired
	public void setSportLablesBiz(MySportLablesBiz sportLablesBiz) {
		this.sportLablesBiz = sportLablesBiz;
	} 
	
	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	/**
	 * 获得所有运动标签图标
	 * @return
	 */
	@Action(value="getallsportlables",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getAllSportLables(){
		map = new HashMap<String, Object>();
		int idongId = sportLables.getIdongId(); // 爱动号
		try {
			if(idongId > 0){
				// 全部运动标签图标
				List<Map<String, Object>> allLableMap = sportLablesBiz.getAllSportLables(); 
				// 我的运动标签图标
				List<Map<String, Object>> myLableMap = sportLablesBiz.getMySportLables(idongId); 
				if(myLableMap.size() > 0){
					for (Map<String, Object> allLable : allLableMap) {
						allLable.put("iconUrl", ReadPropertiesUtil.getIMGREAD(allLable.get("iconUrl")));
						for (Map<String, Object> myLable : myLableMap) {
							if(allLable.get("iconId").equals(myLable.get("tagId"))){
								allLable.put("status", 1); // 选中
								break;
							}else{
								allLable.put("status", 0); // 未选中
							}
						}
					}
				}else{
					for (Map<String, Object> elseMap : allLableMap) {
						elseMap.put("iconUrl", ReadPropertiesUtil.getIMGREAD(elseMap.get("iconUrl")));
						elseMap.put("status", 0);
					}
				}
				map.put("code", 1);
				map.put("result", allLableMap);
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * 添加我的运动标签图标
	 * ANDROID
	 * 先删除在添加
	 * @return
	 */
	@Action(value="addsportlables_android",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addAllSportLablesAndroid(){
		map = new HashMap<String, Object>();
		int idongId = sportLables.getIdongId(); // 爱动号
		String tagIds = sportLables.getTagIds();
		try {
			if(idongId > 0){
				List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
				JSONObject json = new JSONObject(tagIds);
				JSONArray jsonArray = json.optJSONArray("stones");
				
				if(jsonArray.length() > 0){
					Set<String> set = new HashSet<String>();
					for(int i = 0;i < jsonArray.length(); i++){
						JSONObject obj = jsonArray.optJSONObject(i);
						set.add(obj.optString("name"));
					}
					
					for (String string : set) {
						Map<String, Object> sportLablesMap = new HashMap<String, Object>();
						sportLablesMap.put("idongId", idongId);
						sportLablesMap.put("tagId", string);
						sportLablesMap.put("sortId", new Random(100).nextInt());
						listMap.add(sportLablesMap);
					}
					
					sportLablesBiz.addMySportLables(listMap);
					map.put("code", 1);
					map.put("result", "已完成");
					return "succ";
				}else{
					sportLablesBiz.delMySportLables(idongId);
					map.put("code", 1);
					map.put("result", "已完成");
					return "succ";
				}
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
